package com.poizon.utils;

import org.springframework.web.multipart.MultipartFile;

import java.lang.reflect.Field;

/**
 * @ClassName BeanUtils
 * @Author NoCheng
 * @Date 2022/1/2 10:52
 * @Description:TODO 实体类工具
 */

public class BeanUtils {

    public static <T> T beanConvert(Class<T> cls, Object obj, Field[] fields) {
        T t = null;
        try {
            //实例化对象
            t = cls.newInstance();
            //遍历属性
            for (Field f : fields) {
                if (f.getType().equals(MultipartFile.class)){
                    continue;
                }
                //获取属性对象
                Field field = cls.getDeclaredField(f.getName());
                //为属性赋值
                field.setAccessible(true);
                f.setAccessible(true);
                field.set(t, f.get(obj));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return t;
    }

}
